package com.shoppingmall.fragment;

import com.example.shoppingmall.R;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;

public class MainActivity extends FragmentActivity implements
		OnCheckedChangeListener {
	FragmentTransaction ft;
	Fragment_Home mfragment_Home;
	Fragment_PhotoWall mfragment_PhotoWall;
	Fragment_Person mfragment_Person;
	Fragment_More mfragment_More;
	private RadioButton rb_home, rb_photowall, rb_person, rb_more;

	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		// 取消标题
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		init();// 初始化数据
	}

	private void init() {
		// TODO Auto-generated method stub
		rb_home = (RadioButton) findViewById(R.id.rb_home);
		rb_home.setOnCheckedChangeListener(this);
		rb_photowall = (RadioButton) findViewById(R.id.rb_photowall);
		rb_photowall.setOnCheckedChangeListener(this);
		rb_person = (RadioButton) findViewById(R.id.rb_person);
		rb_person.setOnCheckedChangeListener(this);
		rb_more = (RadioButton) findViewById(R.id.rb_more);
		rb_more.setOnCheckedChangeListener(this);
		rb_home.setChecked(true);// 默认选择第一项
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (isChecked) {
			ft = getSupportFragmentManager().beginTransaction();
			switch (buttonView.getId()) {
			case R.id.rb_home:
				if (mfragment_Home == null) {
					mfragment_Home = new Fragment_Home();
					ft.add(R.id.frame_main, mfragment_Home);
				}
				if (mfragment_PhotoWall != null) {
					ft.hide(mfragment_PhotoWall);
				}
				if (mfragment_Person != null) {
					ft.hide(mfragment_Person);
				}
				if (mfragment_More != null) {
					ft.hide(mfragment_More);
				}
				ft.show(mfragment_Home);
				break;
			case R.id.rb_photowall:
				if (mfragment_PhotoWall == null) {
					mfragment_PhotoWall = new Fragment_PhotoWall();
					ft.add(R.id.frame_main, mfragment_PhotoWall);
				}
				if (mfragment_Home != null) {
					ft.hide(mfragment_Home);
				}
				if (mfragment_Person != null) {
					ft.hide(mfragment_Person);
				}
				if (mfragment_More != null) {
					ft.hide(mfragment_More);
				}
				ft.show(mfragment_PhotoWall);
				break;
			case R.id.rb_person:
				if (mfragment_Person == null) {
					mfragment_Person = new Fragment_Person();
					ft.add(R.id.frame_main, mfragment_Person);
				}
				if (mfragment_PhotoWall != null) {
					ft.hide(mfragment_PhotoWall);
				}
				if (mfragment_Home != null) {
					ft.hide(mfragment_Home);
				}

				if (mfragment_More != null) {
					ft.hide(mfragment_More);
				}
				ft.show(mfragment_Person);
				break;

			case R.id.rb_more:
				if (mfragment_More == null) {
					mfragment_More = new Fragment_More();
					ft.add(R.id.frame_main, mfragment_More);
				}
				if (mfragment_PhotoWall != null) {
					ft.hide(mfragment_PhotoWall);
				}
				if (mfragment_Person != null) {
					ft.hide(mfragment_Person);
				}
				if (mfragment_Home != null) {
					ft.hide(mfragment_Home);
				}
				ft.show(mfragment_More);
				break;

			}
			ft.commit();
		}

	}

	/**
	 * back键拦截
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Builder dialog = new AlertDialog.Builder(MainActivity.this);
			dialog.setTitle("提示信息");
			dialog.setMessage("你确定要退出?");
			dialog.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							System.exit(0);
						}
					}).setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
			dialog.show();
		}
		return true;
	}

}
